package com.tao.china.biz.jdbc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.tao.china.biz.jdbc.dao.CatsDao.Cats;
import com.tao.china.biz.jdbc.dao.PropsDao;
import com.tao.china.common.jdbc.DbUtil;
import com.tao.china.taobaoapi.request.Props;
import com.taobao.api.ApiException;
import com.taobao.api.domain.ItemProp;

/**
 * @author liuchao
 */
public class PropsDaoImpl implements PropsDao{
    
    public static final String SQL_INSERT = String.format(
        "insert into %s (%s,%s,%s) values (?,?,?)"  ,
        "props",
        "pid",
        "pname",
        "cid"
          );
    
    Statement stmt = null;
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    
    public void insertProps(Long pid,String pname,Long cid) throws SQLException{
        Connection conn = DbUtil.getConnection();
        ResultSet rs2=null;
        PreparedStatement pstmt1 = null;
        try{
            pstmt1 = conn.prepareStatement(SQL_INSERT);
            System.out.println("--------------insert--------"+cid);
            pstmt1.setLong(1, pid);
            pstmt1.setString(2, pname);
            pstmt1.setLong(3, cid);
            pstmt1.execute();
            pstmt1.close();
            pstmt1=null;
        }
        catch(Exception e)
            {
                e.printStackTrace();
            }
            finally{
                DbUtil.close(conn, null, rs2);
            }
    }
    
    public static void main (String args[]) throws SQLException, ApiException{
        PropsDaoImpl dao = new PropsDaoImpl();
        CatsDaoImpl catsDao = new CatsDaoImpl();
        List<Cats> cats = catsDao.getCats();
        Props props = new Props();
        for(Cats cat : cats){
            List<ItemProp> propsList = props.getCatProps(cat.getCid());
            if(null != propsList){
                for(ItemProp itemPorp : propsList){
                    dao.insertProps(itemPorp.getPid(), itemPorp.getName(), cat.getCid());
                }
            }
        }
    }

}
